﻿using Autofac;
using Autofac.Integration.Mvc;
using log4net;
using LuShanTea.IService;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;

namespace LuShanTea.Admin.Web.Job
{
    public class EveryDayJob : IJob
    {
        ILog log = LogManager.GetLogger(typeof(EveryDayJob));
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                var container = AutofacDependencyResolver.Current.ApplicationContainer;
                using (container.BeginLifetimeScope())
                {
                    var goodService = container.Resolve<IGoodService>();
                    var settingService = container.Resolve<ISettingService>();
                    string bossEmail = settingService.GetSettingByKey("BossEmail").Value;

                    IOrderService orderService = container.Resolve<IOrderService>();
                    long orderCount = orderService.GetCountByDay();
                    StringBuilder sb = new StringBuilder();
                    long goodCount = goodService.GetDayGoodCount();
                    sb.Append("今日新增商品")
                        .Append(goodCount)
                        .Append("件")
                        .Append("。今日完成订单")
                        .Append(orderCount)
                        .Append("个")
                        .Append("。以上是今日的工作汇报，我们将继续努力，为公司身先士卒。----您的员工");

                    SendEmail(bossEmail, sb.ToString());
                }
                log.InfoFormat("给老板发送邮件成功,日期：{0}", DateTime.Now);
            }
            catch (Exception ex)
            {
                log.InfoFormat("给老板发送邮件失败,错误信息：{0}", ex);
            }
        }

        public void SendEmail(string email, string content)
        {
            using (MailMessage mailMessage = new MailMessage())
            using (SmtpClient smtpClient = new SmtpClient("smtp.exmail.qq.com"))
            {
                mailMessage.To.Add(email);
                mailMessage.Body = $"每日工作汇报：{content}";
                mailMessage.From = new MailAddress("zsz@amortal.cn");
                mailMessage.Subject = "公司工作汇报";
                smtpClient.EnableSsl = true;
                smtpClient.Credentials = new System.Net.NetworkCredential("zsz@amortal.cn", "XxoYgKzDyn74a6DP");
                smtpClient.Send(mailMessage);

            }
        }
    }
}